﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace ADayRoiAdmin
{
    public partial class frmList : Form
    {
        SqlConnection con;
        string function = "";
        private BindingManagerBase bm = null;
        AdayroiClass adayroi;
        DataTable dt_product = null;
        public frmList(string function)
        {
            
            this.function = function;
            adayroi = new AdayroiClass(ref con);
            InitializeComponent();
        }

        private void dgv_ProductsLoad()
        {
            DataGridViewTextBoxColumn PKID = new DataGridViewTextBoxColumn();
            PKID.DataPropertyName = "PKID";
            PKID.Name = "PKID";
            PKID.Visible = false;

            DataGridViewTextBoxColumn ProductCode = new DataGridViewTextBoxColumn();
            ProductCode.DataPropertyName = "ProductCode";
            ProductCode.Name = "ProductCode";
            ProductCode.HeaderText = "Mã Sản Phẩm";

            DataGridViewTextBoxColumn ProductName = new DataGridViewTextBoxColumn();
            ProductName.DataPropertyName = "ProductName";
            ProductName.HeaderText = "Tên Sản Phẩm";
            ProductName.Name = "ProductName";

            DataGridViewTextBoxColumn BusinessID = new DataGridViewTextBoxColumn();
            BusinessID.DataPropertyName = "BusinessID";
            BusinessID.Name = "BusinessID";
            BusinessID.Visible = false;

            DataGridViewTextBoxColumn CategoryID = new DataGridViewTextBoxColumn();
            CategoryID.DataPropertyName = "CategoryID";
            CategoryID.Name = "CategoryID";
            CategoryID.Visible = false;

            DataGridViewTextBoxColumn Image = new DataGridViewTextBoxColumn();
            Image.DataPropertyName = "Image";
            Image.Name = "Image";
            Image.HeaderText = "Image";
            Image.Visible = false;

            DataGridViewTextBoxColumn ImageThumbnail = new DataGridViewTextBoxColumn();
            ImageThumbnail.DataPropertyName = "ImageThumbnail";
            ImageThumbnail.Name = "ImageThumbnail";
            ImageThumbnail.HeaderText = "ImageThumbnail";
            ImageThumbnail.Visible = false;

            DataGridViewTextBoxColumn Description = new DataGridViewTextBoxColumn();
            Description.DataPropertyName = "Description";
            Description.Name = "Description";
            Description.HeaderText = "Mô Tả";

            DataGridViewTextBoxColumn Sort = new DataGridViewTextBoxColumn();
            Sort.DataPropertyName = "Sort";
            Sort.Name = "Sort";
            Sort.HeaderText = "Độ Ưu Tiên";

            DataGridViewCheckBoxColumn IsDisplay = new DataGridViewCheckBoxColumn();
            IsDisplay.DataPropertyName = "IsDisplay";
            IsDisplay.Name = "IsDisplay";
            IsDisplay.HeaderText = "Hiển Thị";


            dgv_List.Columns.AddRange(new DataGridViewColumn[] { PKID,ProductCode,ProductName,CategoryID,BusinessID,ImageThumbnail,Image,Description,Sort, IsDisplay });
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                frmProducts product = new frmProducts(0,dt_product.NewRow());
                product.ShowDialog();
                DataRow dr = product._dr_product;
                dt_product.Rows.Add(dr);
                dgv_List.DataSource = dt_product;
                bm = dgv_List.BindingContext[dgv_List.DataSource, dgv_List.DataMember];
            }
        }

        private void frmList_Load(object sender, EventArgs e)
        {
            switch (function)
            {
                case "Products":
                    dt_product = adayroi.getList("select p.*, c.Parent from Products p left join Category c on p.CategoryID = c.PKID", con); 
                    dgv_ProductsLoad();
                    dgv_List.DataSource = dt_product;
                    bm = dgv_List.BindingContext[dgv_List.DataSource, dgv_List.DataMember];
                    break;
            }
        }

        private void dgv_List_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataRow findRow = ((DataRowView)bm.Current).Row;
            int product_id = Convert.ToInt32(findRow["PKID"].ToString());
            frmProducts product = new frmProducts(product_id,findRow);
            product.ShowDialog();
            if (product._deletedrow)
                dt_product.Rows.Remove(findRow);
            else
                findRow.ItemArray = product._dr_product.ItemArray;
        }

        private void dgv_List_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            DialogResult response = MessageBox.Show("Bạn có chắc chắn muốn xóa?", "Xóa dòng?", MessageBoxButtons.YesNo,
                                       MessageBoxIcon.Question,
                                       MessageBoxDefaultButton.Button2);
            if (response == DialogResult.Yes)
            {
                DataRow findRow = ((DataRowView)bm.Current).Row;
                int product_id = Convert.ToInt32(findRow["PKID"].ToString());
                adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/Products/Thumb/", findRow["ImageThumbnail"].ToString());
                adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/Products/", findRow["Image"].ToString());
                string sql = "delete from Products where PKID = " + product_id;
                if (adayroi.UpdateSQL(con, sql) == 0)
                    MessageBox.Show("Thành Công");
            }
            else e.Cancel = true;
        }

       
    }
}
